<?php

// -----------------------------------------------------------------------------

include "config/config.php";
include "global/globalAuth.php";
include "createDBIfNotExists.php";

// 设置时区
ini_set('date.timezone', $timezone);

// 取得今日日期
$today = date('Y-m-d');

// 昨日日期
$yesterday = new DateTime("yesterday");
$yesterday = $yesterday->format("Y-m-d");

// -----------------------------------------------------------------------------

?>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <title>记忆片段</title>
    <link rel="stylesheet" href="resources/bootstrap-4.3.1-dist/css/bootstrap.min.css">
</head>
<body>

<!-- ----------------------------------------------------------------------- -->

<?php include 'global/globalNavBar.php'; ?>

<!-- ----------------------------------------------------------------------- -->

<!--container-->
<div class="container mt-2">

<!-- ----------------------------------------------------------------------- -->

    <div class="list-group">
        <a href="viewByDate.php?date=<?php echo $today; ?>" class="list-group-item list-group-item-action">查看 the 记忆片段 of 今日</a>
        <a href="#" id="checkSpecificDate" class="list-group-item list-group-item-action" data-toggle="modal" data-target="#modalCheckSpecificDate">查看 the 记忆片段 of 指定日期</a>
        <a href="#" class="list-group-item list-group-item-action" data-toggle="modal" data-target="#modalListReviewDates">查看 the 复习日期 by 始记日期</a>
        <a href="editAddWords.php" class="list-group-item list-group-item-action">批量制定单词复习计划</a>
        <a href="editAddArticles.php" class="list-group-item list-group-item-action">批量制定文章复习计划</a>
        <a href="delay.php" class="list-group-item list-group-item-action" data-toggle="modal" data-target="#modalDelay">推迟复习计划</a>
        <a href="edit.php?from=create&dateCreate=<?php echo $today; ?>" class="list-group-item list-group-item-action">新建记忆片段</a>
    </div>

    <div class="list-group mt-2">
        <a href="#" class="list-group-item list-group-item-action" data-toggle="modal" data-target="#modalConfirmResetDB">重置数据库</a>
        <a href="about.php" class="list-group-item list-group-item-action">关于</a>
    </div>

<!-- ----------------------------------------------------------------------- -->

    <!-- Modal -->
    <!-- 查看指定日期的记忆片段 -->
    <div class="modal fade" id="modalCheckSpecificDate" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
        <div class="modal-dialog" role="document">
            <div class="modal-content">

                <div class="modal-header">
                    <h5 class="modal-title" id="exampleModalLabel">查看指定日期的记忆片段</h5>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>

                <div class="modal-body">
                    <div class="mt-2">
                        <input id="inputSpecificDate" type="date" class="form-control" value="<?php echo $today; ?>">
                    </div>
                </div>

                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">取消</button>
                    <button id="btnViewSpecificDate" type="button" class="btn btn-outline-primary">查看</button>
                </div>

            </div>
        </div>
    </div>


    <!-- Modal -->
    <!-- 按始记日期查看复习日期 -->
    <div class="modal fade" id="modalListReviewDates" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
        <div class="modal-dialog" role="document">
            <div class="modal-content">

                <div class="modal-header">
                    <h5 class="modal-title" id="exampleModalLabel">请指定始记日期：</h5>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>

                <div class="modal-body">
                    <div class="mt-2">
                        <input id="inputStartDate" type="date" class="form-control" value="<?php echo $today; ?>">
                    </div>
                </div>

                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">取消</button>
                    <button id="btnListReviewDates" type="button" class="btn btn-outline-primary">查看</button>
                </div>

            </div>
        </div>
    </div>


    <!-- Modal -->
    <!-- 推迟复习计划 -->
    <div class="modal fade" id="modalDelay" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
        <div class="modal-dialog" role="document">
            <div class="modal-content">

                <div class="modal-header">
                    <h5 class="modal-title" id="exampleModalLabel">推迟复习计划</h5>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>

                <div class="modal-body">
                    <div class="mt-2">

                        <div class="input-group mb-3">
                            <div class="input-group-prepend">
                                <span class="input-group-text" id="basic-addon3">原定始记日期</span>
                            </div>
                            <input type="date"
                                   id="inputPlannedDate"
                                   value="<?php echo $yesterday; ?>"
                                   class="form-control"
                                   aria-describedby="basic-addon3">
                        </div>

                        <div class="input-group mb-3">
                            <div class="input-group-prepend">
                                <span class="input-group-text" id="basic-addon3">实际始记日期</span>
                            </div>
                            <input type="date"
                                   id="inputActualDate"
                                   value="<?php echo $today; ?>"
                                   class="form-control"
                                   aria-describedby="basic-addon3">
                        </div>

                    </div>
                </div>

                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">取消</button>
                    <button id="btnDelay" type="button" class="btn btn-outline-primary">确认</button>
                </div>

            </div>
        </div>
    </div>


    <!-- Modal -->
    <!-- 确认重置数据库 -->
    <div class="modal fade" id="modalConfirmResetDB" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
        <div class="modal-dialog" role="document">
            <div class="modal-content">

                <div class="modal-header">
                    <h5 class="modal-title" id="exampleModalLabel">确认重置数据库？</h5>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>

                <div class="modal-body">

                    <div class="mt-2">
                        <p>请在下方输入框中输入「sure」以确认删除。</p>
                        <p>该操作无法撤销，请谨慎选择。</p>
                        <input id="inputSure" type="text" class="form-control">
                    </div>

                </div>

                <div class="modal-footer">
                    <button type="button" class="btn btn-outline-secondary" data-dismiss="modal">取消</button>
                    <button id="btnResetDB" type="button" class="btn btn-outline-primary">确定</button>
                </div>

            </div>
        </div>
    </div>
<!-- ----------------------------------------------------------------------- -->

</div>
<!--container-->

<!-- ----------------------------------------------------------------------- -->
<script src="resources/jquery/jquery-3.4.0.min.js"></script>
<script src="resources/bootstrap-4.3.1-dist/js/bootstrap.min.js"></script>
<!-- ----------------------------------------------------------------------- -->

<script>
    $("#btnViewSpecificDate").click(function () {
        var date = $("#inputSpecificDate").val()
        var url = "viewByDate.php?date=" + date
        $("#modalCheckSpecificDate").modal("hide")
        window.open(url, "_self")
    })
</script>

<!-- ----------------------------------------------------------------------- -->

<script>
    $("#btnListReviewDates").click(function () {
        var startDate = $("#inputStartDate").val()
        var url = "listReviewDates.php?date=" + startDate
        $("#modalListReviewDates").modal("hide")
        window.open(url, "_self")
    })
</script>

<!-- ----------------------------------------------------------------------- -->

<script>
    $("#btnDelay").click(function () {
        var plannedDate = $("#inputPlannedDate").val()
        var actualDate = $("#inputActualDate").val()
        var url = `delay.php?plannedDate=${plannedDate}&actualDate=${actualDate}`
        window.open(url, "_self")
    })
</script>

<!-- ----------------------------------------------------------------------- -->

<script>
    $("#btnResetDB").click(function () {
        var inputtedContent = $("#inputSure").val()
        $("#inputSure").val("")

        if (inputtedContent == "sure") {

            var url = "DBReset.php"
            $("#modalConfirmResetDB").modal("hide")

            var request = $.ajax({
                url: url,
                method: "GET",
                dataType: "html"
            });

            request.done(function(msg) {
                alert(msg)
            });

            request.fail(function(jqXHR, textStatus) {
                alert( "请求失败：" + textStatus );
            });

        }
    })
</script>

<!-- ----------------------------------------------------------------------- -->

</body>
</html>
